package main

import (
	"encoding/json"
	"fmt"
	"github.com/gocolly/colly"
	"github.com/gocolly/colly/extensions"
	"github.com/xuri/excelize/v2"
	"log"
	"math/rand"
	"strconv"
	"time"
)

func main() {

	c := colly.NewCollector(func(collector *colly.Collector) {
		extensions.RandomUserAgent(collector)
	})
	c.OnRequest(func(request *colly.Request) {

		request.Headers.Set("authority", "api.zsxq.com")
		request.Headers.Set("methed", "GET")
		request.Headers.Set("path", "/v2/hashtags/28851422588481/topics?count=20")
		request.Headers.Set("accept-encoding", "gzip, deflate, br")
		request.Headers.Set("accept-language", "zh-CN,zh;q=0.9")
		request.Headers.Set("cache-control", "max-age=0")
		request.Headers.Set("accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9")
		request.Headers.Set("cookie", "sensorsdata2015jssdkcross=%7B%22distinct_id%22%3A%22185552885221422%22%2C%22first_id%22%3A%221844199201716b-0e469598c1d7f78-26021e51-1327104-184419920183d0%22%2C%22props%22%3A%7B%22%24latest_traffic_source_type%22%3A%22%E5%BC%95%E8%8D%90%E6%B5%81%E9%87%8F%22%2C%22%24latest_search_keyword%22%3A%22%E6%9C%AA%E5%8F%96%E5%88%B0%E5%80%BC%22%2C%22%24latest_referrer%22%3A%22https%3A%2F%2Fbcdh.yuque.com%2F%22%7D%2C%22identities%22%3A%22eyIkaWRlbnRpdHlfY29va2llX2lkIjoiMTg0NDE5OTIwMTcxNmItMGU0Njk1OThjMWQ3Zjc4LTI2MDIxZTUxLTEzMjcxMDQtMTg0NDE5OTIwMTgzZDAiLCIkaWRlbnRpdHlfbG9naW5faWQiOiIxODU1NTI4ODUyMjE0MjIifQ%3D%3D%22%2C%22history_login_id%22%3A%7B%22name%22%3A%22%24identity_login_id%22%2C%22value%22%3A%22185552885221422%22%7D%2C%22%24device_id%22%3A%221844199201716b-0e469598c1d7f78-26021e51-1327104-184419920183d0%22%7D; abtest_env=product; zsxq_access_token=2B13CB3B-51A0-B0C5-62AA-1EBD24D87255_B6635E52CD7565A4; zsxqsessionid=554d06615249e88c2aa9b02da3707bd9")
		request.Headers.Set("user-agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/108.0.0.0 Safari/537.36")
		fmt.Printf("fetch --->%s\n", request.URL.String())
	})
	c.OnResponse(func(response *colly.Response) {
		defer func() {
			if err := recover(); err != nil {
				fmt.Println(err)
			}
		}()

		var f interface{}
		//反序列化
		err := json.Unmarshal(response.Body, &f)
		if err != nil {
			return
		}
		// 打印响应数据
		//fmt.Println(f)

		//如果返回错误跳出
		if f.(map[string]interface{})["succeeded"] != true {
			log.Fatal("succeeded：false")
			//panic("请求失败")
			return
		}
		res := f.(map[string]interface{})["resp_data"]
		data := res.(map[string]interface{})["topics"]
		var i = 1
		file := excelize.NewFile()
		// Create a new sheet.
		index := file.NewSheet("Sheet1")
		for _, v := range data.([]interface{}) {
			owner := v.(map[string]interface{})["talk"].(map[string]interface{})["owner"]
			userId := owner.(map[string]interface{})["user_id"]
			name := owner.(map[string]interface{})["name"]
			location := owner.(map[string]interface{})["location"]
			text := v.(map[string]interface{})["talk"].(map[string]interface{})["text"]
			fmt.Println(userId, "\n", name, "\n", location, "\n", text)

			// Set value of a cell.
			strIndex := strconv.Itoa(i)
			id := "A" + strIndex
			pos := "B" + strIndex
			mark := "C" + strIndex
			des := "D" + strIndex
			rand.Seed(time.Now().UnixNano())
			r := rand.Intn(100)
			file.SetCellValue("Sheet1", mark, r)
			file.SetCellValue("Sheet1", id, i)
			file.SetCellValue("Sheet1", pos, name)
			file.SetCellValue("Sheet1", des, text)
			// Set active sheet of the workbook.
			file.SetActiveSheet(index)
			i++
		}
		// Save spreadsheet by the given path.
		if err := file.SaveAs("userInfo.xlsx"); err != nil {
			fmt.Println(err)
		}

	})
	c.Visit("https://api.zsxq.com/v2/hashtags/28851422588481/topics?count=30")
}
